<template>
  <div>
    <el-form ref="myform" :model="formItem" label-width="100px" size="mini" class="form-view">
      <el-row :gutter="40">
        <el-col :span="12">
          <el-form-item label="标题">
            {{ formItem.title }}
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="申请服务厅局">
            {{ formItem.serviceUnitName }}
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="事件等级">
            {{ getEventLevel(formItem.eventLevel) }}
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="一线工程师">
            {{ formItem.frontlineEngineerName }}
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="参与工程师">
            {{ formItem.helperEngineerName }}
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="客户姓名">
            {{ formItem.customerName }}
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="客户电话">
            {{ formItem.customerPhone }}
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="处室">
            {{ formItem.place }}
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="房间号">
            {{ formItem.roomNumber }}
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="是否备份">
            {{ formItem.whetherBackup === true ? '是':'否' }}
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="是否涉密">
            {{ formItem.whetherSecret === true ? '是':'否' }}
          </el-form-item>
        </el-col>
        <!-- <el-col :span="12">
          <el-form-item label="故障级别">
            {{ getFailureLevel(formItem.whetherSecret) }}
          </el-form-item>
        </el-col> -->
        <el-col :span="24">
          <el-form-item label="变更描述">
            <div class="height-div">
              {{ formItem.eventDescription }}
            </div>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="附件">
            <el-button v-for="tag in formItem.annex" :key="tag.name" size="mini" type="text" @click.native="downloadFile(tag)">{{ tag.name }}</el-button>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
  </div>
</template>

<script>
import { detail, applyformDownloadFile as downloadFile } from '@neu/dcom-ui/api/alter'
import { list as dList } from '@neu/dcom-ui/api/dictionary'
// import { findByDictionaryType } from '@neu/dcom-ui/api/dictionary'

const formItem = {
  // 标题
  title: '',
  // 工作流TaskId
  taskId: '',
  // 工单号
  workNum: '',
  // 申请服务厅局名称
  serviceUnitName: '',
  // 申请服务厅局编码
  serviceUnitCode: '',
  // 一线工程师编码
  frontlineEngineerCode: '',
  // 一线工程师名称
  frontlineEngineerName: '',
  // 客户姓名
  customerName: '',
  // 客户电话
  customerPhone: '',
  // 处室
  place: '',
  // 房间号
  roomNumber: '',
  // 是否备份
  whetherBackup: true,
  // 是否涉密
  whetherSecret: false,
  // 事件级别
  eventLevel: '',
  // 事件描述
  eventDescription: '',
  // 状态
  status: 'temporary',
  // 附件
  annex: []
}

export default {
  name: 'MalfunctionApplyForm',
  props: {
    taskId: {
      required: false,
      type: String,
      default: 'NULL'
    },
    workOrderNum: {
      required: false,
      type: String,
      default: ''
    }
  },
  data() {
    return {
      // 故障级别集合
      // availableFailureLevels: [],
      // 编辑的表单内容
      formItem: {
        ...formItem
      },
      eventgradeList: []
    }
  },
  created() {
    this.fetch()
    // 故障级别
    // findByDictionaryType('failureLevel').then(response => {
    //   this.availableFailureLevels = response.data
    // })
    // 事件等级
    dList({ s_type: 'eventgrade' }).then(res => {
      this.eventgradeList = res.data
    })
  },
  methods: {
    fetch() {
      if (this.workOrderNum !== 'NULL') {
        detail(this.workOrderNum, this.taskId).then(response => {
          this.formItem = {
            ...response.data
          }
        })
      } else {
        this.formItem = {
          ...formItem
        }
      }
    },
    /** 下载附件 */
    downloadFile(tag) {
      downloadFile(tag.fsId).then(res => {
        const content = res
        const blob = new Blob([content])
        const fileName = tag.name
        if ('download' in document.createElement('a')) { // 非IE下载
          const elink = document.createElement('a')
          elink.download = fileName
          elink.style.display = 'none'
          elink.href = URL.createObjectURL(blob)
          document.body.appendChild(elink)
          elink.click()
          URL.revokeObjectURL(elink.href) // 释放URL 对象
          document.body.removeChild(elink)
        } else { // IE10+下载
          navigator.msSaveBlob(blob, fileName)
        }
      })
    },
    getEventLevel(code) {
      const one = this.eventgradeList.find(e => e.code === code)
      return one ? one.text : ''
    }
  }
}
</script>

<style lang="scss" scoped>
.height-div {
    min-height: 80px;
    max-height: 100px;
}
</style>

